﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using forum.Data;
using forum.ApplicationCode;

namespace forum
{
    public partial class SubForum : System.Web.UI.Page
    {
        int SubForumId
        {
            get
            {
                if (Request.QueryString["SubForumId"] != null)
                {
                    int subForumId;
                    if (int.TryParse(Request.QueryString["SubForumId"], out subForumId))
                    {
                        return subForumId;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            

            // Reload the page, not from broswer's cache
            Response.AppendHeader("Cache-Control", "no-store");
            if(Membership.GetUserId()!=0)
                  clearUnreadThreads();

            if (Membership.GetUserName() == "")
                lnkbtnAddThread.Visible = false;
            hdnfldForumId.Value = SubForumId.ToString();

            using (forumDataContext data = new forumDataContext())
            {
                var forum = data.sp_GetSubForumName(SubForumId);
                if (forum == null)
                    Response.Redirect("forumWelcome.asp");
                lblSubForumeName.Text = forum.FirstOrDefault().subForumTopic;

                lvSubForumModerators.DataSource = data.sp_GetSubForumModeratoes(SubForumId);
                lvSubForumModerators.DataBind();

                lvThreads.DataSource = data.sp_GetSubForumThreads(SubForumId);
                lvThreads.DataBind();
               
            }

        }

        private void clearUnreadThreads()
        {
            if (Session["unreadThreads"] != null)
            {
                List<int> unreadThreads = Session["unreadThreads"] as List<int>;
                List<int> newUnreadThreads = new List<int>();
                foreach (int thread in unreadThreads)
                {
                    using (forumDataContext data = new forumDataContext())
                    {
                        int isBelongToSubForum = data.sp_IsThreadBelongsToSubForum(SubForumId, thread).FirstOrDefault().Column1.Value;
                        if(isBelongToSubForum==0)
                            newUnreadThreads.Add(thread);
                    }
                }
                Session["unreadThreads"] = newUnreadThreads;
            }
        }


        protected void lvThreads_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            using (forumDataContext data = new forumDataContext())
            {
                int threadId = ((sp_GetSubForumThreadsResult)DataBinder.GetDataItem(e.Item)).threadId;
                HyperLink linkEdit = e.Item.FindControl("hyplnkEdit") as HyperLink;
                HyperLink linkdel = e.Item.FindControl("lbkbtnDelete") as HyperLink;

                if (!isAllowEdit(threadId))
                {
                    linkEdit.Visible = false;
                    linkdel.Visible = false;

                }
                
            }

        }

        protected Boolean isAllowEdit(int threadId)
        {

            Boolean isOwner = false;
            using (forumDataContext data = new forumDataContext())
            {
                if (data.sp_IsOwnerOfThread(Membership.GetUserId(), threadId).FirstOrDefault().isOwner > 0)
                    isOwner = true;

            }
            return (isOwner);


           /* Boolean isAdmin = Membership.IsAdmin();
            Boolean isModerator = Membership.IsModerator(SubForumId);
            Boolean isOwner = false;
            using (forumDataContext data = new forumDataContext())
            {
                if (data.sp_IsOwnerOfThread(Membership.GetUserId(), threadId).FirstOrDefault().isOwner > 0)
                    isOwner = true;

            }

            return (isAdmin || isModerator || isOwner);
            * */
        }   


    }
}